package com.edie.customview.modular;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.edie.customview.R;
import com.edie.customview.helper.BottomNavigationViewHelper;

public class BottomNavigationActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "BottomNavigationActivit";
    public static final int[] mTabRes = new int[]{R.drawable.tab_home_selector, R.drawable.tab_home_selector, R.drawable.tab_home_selector, R.drawable.tab_home_selector, R.drawable.tab_home_selector};
    //    public static final int []mTabResPressed = new int[]{R.drawable.ic_tab_strip_icon_feed_selected,R.drawable.ic_tab_strip_icon_category_selected,R.drawable.ic_tab_strip_icon_pgc_selected,R.drawable.ic_tab_strip_icon_profile_selected};
    public static final String[] mTabTitle = new String[]{"首页", "发现", "关注", "关注", "我的"};
    private BottomNavigationView bottomNavigationView;
    private TabLayout mTabLayout;
    private LayoutInflater mFrom;
    private TextView mTextView;
    private Button button4;
    private Button button5;

    public static void start(Context context) {
        context.startActivity(new Intent(context, BottomNavigationActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_navigation);
        initView();
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        //默认 >3 的选中效果会影响ViewPager的滑动切换时的效果，故利用反射去掉
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

        mTabLayout = (TabLayout) findViewById(R.id.tab);
        mTextView = (TextView) findViewById(R.id.text);
        inittext();

        mFrom = LayoutInflater.from(this);


        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                Log.i(TAG, "onTabSelected: " + position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabView(this, 0)));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabView(this, 1)));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabView(this, 2)));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabView(this, 3)));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(getTabView(this, 4)));
    }

    private void inittext() {

        mTextView.setText("这是一条测试文本");
        SpannableString clickString = new SpannableString("可点击文本");
        clickString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "可点击文本", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.RED);//设置颜色
            }
        }, 0, clickString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.append(clickString);
        mTextView.append(new SpannableString("这也是一条测试文本"));
        SpannableString clickString2 = new SpannableString("我是可点击的");
        clickString2.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "我是可点击的", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE); //设置颜色
            }
        }, 0, clickString2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.append(clickString2);
        mTextView.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }

    /**
     * 获取Tab 显示的内容
     *
     * @param context
     * @param position
     * @return
     */
    public View getTabView(Context context, int position) {

        View view = mFrom.inflate(R.layout.item_ne, null);
        ImageView tabIcon = (ImageView) view.findViewById(R.id.tab_content_image);
        tabIcon.setImageResource(mTabRes[position]);
        TextView tabText = (TextView) view.findViewById(R.id.tab_content_text);
        tabText.setText(mTabTitle[position]);
        return view;
    }

    private void initView() {
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);

        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button4:
                jian44();
                break;
            case R.id.button5:
                app55();
                break;
        }
    }

    private void jian44() {
        mTextView.setText("这是一条测试文本");
        SpannableString clickString = new SpannableString("可点击文本");
        clickString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "可点击文本", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.RED);//设置颜色
            }
        }, 0, clickString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mTextView.append(clickString);
        mTextView.append(new SpannableString("这也是一条测试文本"));
//                mTextView.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }

    private void app55() {
        mTextView.setText("这是一条测试文本");
        SpannableString clickString = new SpannableString("可点击文本");
        clickString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "可点击文本", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.RED);//设置颜色
            }
        }, 0, clickString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.append(clickString);
        mTextView.append(new SpannableString("这也是一条测试文本"));
        SpannableString clickString2 = new SpannableString("我是可点击的");
        clickString2.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "我是可点击的", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE); //设置颜色
            }
        }, 0, clickString2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.append(clickString2);

        SpannableString clickString3 = new SpannableString("我是可点击的");
        clickString3.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Toast.makeText(getApplicationContext(), "我是测试文本可点击的", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.YELLOW); //设置颜色
            }
        }, 0, clickString3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTextView.append(clickString3);
//                mTextView.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }
}
